//
//  SWAccount.swift
//  MissMedia
//
//  Created by linhan on 16/4/1.
//  Copyright © 2016年 Miss. All rights reserved.
//

import Foundation

extension SWToken
{
    convenience init?(user:WeiboSignInUser)
    {
        self.init()
        if user.accessToken == "" || user.uid == "" {
            return nil
        }
        token = user.accessToken
        uid = user.uid
    }
    
    convenience init?(user:Wechat.User)
    {
        self.init()
        guard let accessToken = user.accessToken , accessToken != "" && user.openID != "" else {
            return nil
        }
        token = accessToken
        uid = user.openID
    }
    
    convenience init?(user:QQSDK.User)
    {
        self.init()
        guard let accessToken = user.accessToken , accessToken != "" && user.openID != "" else {
            return nil
        }
        token = accessToken
        uid = user.openID
    }
    
    convenience init?(response:SWAuthDataRsp)
    {
        self.init()
        guard let token = response.token,
              let uid = response.uid , token != "" && uid > 0 else {
                return nil
        }
        self.token = token
        self.uid = String(uid)
    }
    
    
}

extension SWConnectionAccount
{
    convenience init(user:WeiboSignInUser)
    {
        self.init()
        uid = user.uid
        unionID = user.uid
        screenName = user.screenName
        avatarURL = user.avatarURL
        gender = SWGender.getGender(user.gender)
        location = user.location
        token = SWToken(user: user)
    }
    
    convenience init(user:Wechat.User)
    {
        self.init()
        uid = user.openID
        unionID = user.unionID
        screenName = user.screenName
        avatarURL = user.avatarURL
        gender = SWGender.getGender(user.gender)
        location = (user.province ?? "") + " " + (user.city ?? "")
        token = SWToken(user: user)
    }
    
    convenience init(user:QQSDK.User)
    {
        self.init()
        uid = user.openID
        unionID = user.openID
        screenName = user.screenName
        avatarURL = user.avatarURL
        gender = SWGender.getGender(user.gender)
        location = (user.province ?? "") + " " + (user.city ?? "")
        token = SWToken(user: user)
    }
    
    
    
}

extension SWMainAccount
{
    convenience init?(response:SWAuthDataRsp)
    {
        self.init()
        guard let token = SWToken(response: response) else {
                return nil
        }
        self.token = token
        
        uid = token.uid
        screenName = response.screenName
        avatarURL = response.avatar
        desc = response.desc
        birthday = response.birthday
        if let childRsp = response.child,
           let child = SWChild(child:childRsp)
        {
            children = [child]
        }
        //gender = SWGender.getGender(responseValue:userInfo?.gender)
        //familyID = "\((userInfo?.familyID ?? 0))"
    }
    
    func update(response:SWAuthDataRsp)
    {
        screenName = response.screenName
        avatarURL = response.avatar
        desc = response.desc
        birthday = response.birthday
        if let childRsp = response.child,
            let child = SWChild(child:childRsp)
        {
            children = [child]
        }
    }
    
    //是否有效
    var familyIDValid:Bool{
        if let id = familyID , id != "0" && id != ""{
            return true
        }
        return false
    }
    
    var child:SWChild?{
        return children?.valueAt(0) as? SWChild
    }
    
}

extension SWChild
{
    convenience init?(child:SWChildRsp)
    {
        self.init()
        guard let uid = child.uid , uid != 0 else {
            return nil
        }
        self.uid = "\(uid)"
        screenName = child.screenName
        infantName = child.infantName
        birthday = child.birthday
        avatarURL = child.avatar
        gender = SWGender.getGender(responseValue:child.gender)
    }
    
    var days:Int{
        let now = Date()
        if let date = birthday,now.timeIntervalSince1970 > date.timeIntervalSince1970{
            let secondsInDay:Double = 3600 * 24
            //不满一天算一天
            return Int((now.timeIntervalSince1970 - date.timeIntervalSince1970) / secondsInDay) + 1
        }
        return 0
    }
    
    //几周几天
    var daysString:String{
        let days:Double = Double(self.days)
        if days >= 7
        {
            let remainder:Int = Int(days.truncatingRemainder(dividingBy: 7))
            return remainder == 0 ? "\(Int(days / 7))周" : "\(Int(days / 7))周\(remainder)天"
        }
        else
        {
            return "\(Int(days))天"
        }
    }
    
}

extension SWGender
{
    static func getGender(_ gender:Wechat.Gender) -> SWGender
    {
        return SWGender(rawValue: gender.rawValue) ?? .Unknow
    }
    
    static func getGender(_ gender:QQSDK.Gender) -> SWGender
    {
        return SWGender(rawValue: gender.rawValue) ?? .Unknow
    }
    
    static func getGender(rawValue:Int) -> SWGender
    {
        return SWGender(rawValue: rawValue) ?? .Unknow
    }
    
    static func getGender(_ genderString:String) -> SWGender
    {
        var value:SWGender = .Unknow
        if genderString == "m"
        {
            value = .Male
        }
        else if genderString == "f"
        {
            value = .Female
        }
        return value
    }
    
    //性别：1 男，2 女，3 未知
    static func getGender(responseValue:Int?) -> SWGender
    {
        guard let gender = responseValue else{
            return .Unknow
        }
        
        var value:SWGender = .Unknow
        if gender == 1
        {
            value = .Male
        }
        else if gender == 2
        {
            value = .Female
        }
        return value
    }
    
}

extension SWGender
{
    var descriptionString:String{
        switch self {
        case .Unknow: return "未知"
        case .Female: return "女性"
        case .Male: return "男性"
        }
    }
    
    var responseValue:Int{
        switch self {
        case .Unknow: return 3
        case .Female: return 2
        case .Male: return 1
        }
    }
}

extension SWRole
{
    var responseValue:Int{
        switch self {
        case .Father: return 1
        case .Mother: return 2
        case .Grandpa: return 3
        case .Grandma: return 4
        case .Uncle: return 5
        case .Aunt: return 6
        case .Brother: return 7
        case .Grandmother: return 5
        case .Grandfather: return 6
        case .Other: return 99
        }
    }
    
    var roleName:String{
        switch self {
        case .Father: return "爸爸"
        case .Mother: return "妈妈"
        case .Grandpa: return "爷爷"
        case .Grandma: return "奶奶"
        case .Uncle: return "叔叔"
        case .Aunt: return "阿姨"
        case .Brother: return "哥哥"
        case .Grandmother: return "外婆"
        case .Grandfather: return "外公"
        case .Other: return "其他"
        }
    }
    
}

enum ConnectionPlatform:String
{
    case Default = "default"
    case Phone = "phone"
    case QQ = "1"
    case Wechat = "2"
    case Weibo = "3"
}
